<template>
  <el-dialog
    :title="'代码生成器'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="作者" prop="author">
      <el-input v-model="dataForm.author" placeholder="author name"></el-input>
    </el-form-item>
    <el-form-item label="模块名" prop="moduleName">
      <el-input v-model="dataForm.moduleName" placeholder="generate"></el-input>
    </el-form-item>
    <el-form-item label="主路径" prop="mainPath">
      <el-input v-model="dataForm.mainPath" placeholder="com.fast"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="xxx@email.com"></el-input>
    </el-form-item>
    <el-form-item label="表前缀" prop="tablePrefix">
      <el-input v-model="dataForm.tablePrefix" placeholder="tb_"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          author: '',
          moduleName: '',
          mainPath: '',
          email: '',
          tablePrefix: '',
          tableNames: ''
        },
        dataRule: {
          author: [
            { required: true, message: '作者不能为空', trigger: 'blur' }
          ],
          moduleName: [
            { required: true, message: '模块名不能为空', trigger: 'blur' }
          ],
          mainPath: [
            { required: true, message: '主路径不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (item) {
        this.visible = true
        var tables = '';
        for (var i = 0; i < item.length; i++) {
          if (i === 0) {
            tables = item[0]
          } else {
            tables = tables + ',' + item[i]
          }
        }
        this.dataForm.tableNames = tables
        debugger
      },
      // 表单提交
      dataFormSubmit () {
        var obj = this.dataForm
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 需要进行请求的url地址
            var url = this.$http.adornUrl(`/sys/generator/code?`);
            //遍历整个对象
            for (let key in obj) {
              // 如果该项的obj不为空（等于0也可以），并且是第一个不为空的参数时，直接进行拼接，不用加&
              if (
                (url === this.$http.adornUrl(`/sys/generator/code?`) && obj[key]) ||
                (url === this.$http.adornUrl(`/sys/generator/code?`) && obj[key] === 0)
              ) {
                url = url + key + '=' + obj[key]
                // 如果该项的obj不为空（等于0也可以），但不是第一个不为空的参数时，加&进行拼接
              } else if (
                (url !== this.$http.adornUrl(`/sys/generator/code?`) && obj[key]) ||
                (url !== this.$http.adornUrl(`/sys/generator/code?`) && obj[key] === 0)
              ) {
                url = url + '&' + key + '=' + obj[key]
              }
            }
            console.log(url);
            window.location.href = url;
          }
        })
        this.visible = false
      }
    }
  }
</script>
